<template>
  <div>
    <TopFilter :list="topFilterList" :cols="4" @filterChange="changeHandle" @onCollapse="collapseHandle" />

    <BaseDialog :visible.sync="hour" :title="'完工调整'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <closeAssignModel :roNo="partColumns" :tiaoZheng= "tiaoZheng" @close="closeDialog" />
    </BaseDialog>

    <BaseDialog :visible.sync="labour" title="明细" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <labourModel :list="this.partColumns" @closeDialog="closeDialog"></labourModel>
    </BaseDialog>

    <FilterTable
      ref="workAdjustTable"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      columnsRef="workAdjustTables"
      selectionType="single"
      height="350"
      :datakey="'rows'"
      :onRowSelectChange="columns => (this.partColumns = columns)"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <el-button size="small" type="primary" @click="hourClick">完工调整</el-button>
      </template>
    </FilterTable>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { queryplantHourAdjust } from '@/api/repair/repairAssign/workAdjust';
import { getPinpSelect, getChexSelect, getCheModelSelect } from '@/api/repair/repairReception/customer/brand.js';
import { searchBrand, searchSeries, searchModel, searchApAckAge } from '@/api/customerRepair/customerComplaint/customerComplaintReception';
import moment from 'moment';
import { searchRoleByCode } from '@/api/common/empSelect/index';
import dictCode from '@/utils/dictCode';
import closeAssignModel from '@/pages/repair/workManage/repairDispatch/closeAssignModel';
import labourModel from '@/pages/repair/repairReception/repairBusinessManger/labourModel';
import { getfindAllRepairTypeList } from '@/api/repair/repairReception/evaluation/index';
export default {
  name: 'workAdjust',
  components: {
    closeAssignModel,
    labourModel
  },
  mixins: [dictionary],
  data() {
    return {
      tiaoZheng : "wanGong",
      topFilterList: this.createTopFilterList(),
      fetchParams: { xhrAbort: true },
      columns: this.createTableColumns(),
      fetchApi: queryplantHourAdjust,
      hour: false,
      labour: false,
      partColumns: []
    };
  },
  mounted() {
    this.BaseTable = this.$refs.workAdjustTable.$refs.pageTable;
    this.serviceConsultant();
    this.getBrand();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '日期类型',
          fieldName: 'queryDateType',
          placeholder: '请选择派工状态',
          initialValue: '1',
          itemList: [
            { text: '工单开单日期', value: '1' },
            { text: '提交结算时间', value: '2' },
            { text: '竣工日期', value: '3' },
            { text: '交车日期', value: '4' }
          ]
        },
        {
          type: 'RANGE_DATE',
          label: '选择日期',
          fieldName: 'begindate|enddate',
          style: { minWidth: '200px' },
          valueFormat: 'yyyy-MM-dd'
        },
        {
          type: 'INPUT',
          label: '工单号',
          fieldName: 'roNo',
          placeholder: '请输入工单号'
        },
        // {
        //   type: 'SELECT',
        //   label: '工单类型',
        //   fieldName: 'roType',
        //   placeholder: '请选择工单类型',
        //   itemList: this.createDictList('8068')
        // },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'license',
          placeholder: '请输入车牌号',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '品牌',
          fieldName: 'brand',
          placeholder: '请选择品牌',
          change: val => {
            this.changePinpSelect(val);
          },
          itemList: []
        },
        {
          type: 'SELECT',
          label: '车系',
          fieldName: 'series',
          placeholder: '请选择车系',
          change: val => {
            this.changeChexSelect(val);
          },
          itemList: []
        },
        {
          type: 'SELECT',
          label: '车型',
          fieldName: 'model',
          placeholder: '请选择车型',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '服务顾问',
          fieldName: 'serviceAdvisor',
          placeholder: '请选择服务顾问',
          itemList: []
        },
        {
          type: 'SELECT',
          label: '维修类型',
          fieldName: 'repairtype',
          placeholder: '请选择维修类型',
          itemList: this.createDictList('8000')
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '明细',
          dataIndex: 'mx',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <el-button icon="el-icon-document" onClick={() => this.repairLabour(props)}></el-button>;
          }
        },
        {
          title: '工单状态',
          dataIndex: 'RO_STATUS',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{this.createDictText(props.row.RO_STATUS, '8049')}</span>;
          }
        },
        {
          title: '开单日期',
          dataIndex: 'RO_CREATE_DATE',
          sorter: true,
          filter: true,
          width: 150,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          title: '工单号',
          dataIndex: 'RO_NO',
          sorter: true,
          filter: true,
          width: 150,
          filterType: 'input'
        },
        // {
        //   title: '工单类型',
        //   dataIndex: 'RO_TYPE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '车系',
        //   dataIndex: 'SERIES',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: '服务顾问',
          dataIndex: 'SERVICE_ADVISOR',
          sorter: true,
          disabled: true,
          editable: true,
          editType: 'select',
          editItems: []
        },
        {
          title: '车牌号',
          dataIndex: 'LICENSE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '品牌',
          dataIndex: 'BRAND_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车系',
          dataIndex: 'SERIES_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型',
          dataIndex: 'MODEL_NAME',
          sorter: true,
          filter: true,
          width: 150,
          filterType: 'input'
        },
        {
          title: '维修类型',
          dataIndex: 'REPAIR_TYPE_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车主',
          dataIndex: 'OWNER_NAME',
          sorter: true,
          filter: true,
          width: 100,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '送修人',
          dataIndex: 'DELIVERER',
          sorter: true,
          filter: true,
          width: 100,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '送修人电话',
          dataIndex: 'DELIVERER_PHONE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '预交车时间',
          dataIndex: 'END_TIME_SUPPOSED',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          title: '竣工 时间',
          dataIndex: 'COMPLETE_TIME',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          title: '提交结算时间',
          dataIndex: 'FOR_BALANCE_TIME',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          title: '交车 日期',
          dataIndex: 'DELIVERY_DATE',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd HH:mm:ss'
        },
        {
          title: '车主编号',
          dataIndex: 'OWNER_NO',
          sorter: true,
          filter: true,
          width: 150,
          filterType: 'input'
        },
        {
          title: '锁定人',
          dataIndex: 'LOCK_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备注',
          dataIndex: 'REMARK',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
        // {
        //   title: '进厂行驶里程',
        //   dataIndex: 'IN_MILEAGE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        // {
        //   title: '出厂行驶里程',
        //   dataIndex: 'OUT_MILEAGE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // }
      ];
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val, { xhrAbort: false });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    repairLabour(props) {
      this.partColumns = props.row;
      this.labour = true;
    },
    hourClick() {
      if (this.partColumns.length <= 0) {
        this.$notify.error({ title: '提示信息', message: '请先选择数据!' });
      } else {
        this.hour = true;
      }
    },
    async completedClick() {
      if (this.partColumns[0].COMPLETE_TAG == 12781002) {
        this.completed = true;
      } else {
        await maintainRepairBack(this.partColumns.RO_NO);
      }
    },
    refreshTable() {
      this.fetchParams = Object.assign({}, this.fetchParams); // 刷新表格
    },
    // 售前人员接口数据
    async serviceConsultant() {
      this.topFilterList.find(item => item.fieldName === 'begindate|enddate').initialValue = [moment(new Date()).format('YYYY-MM-DD'), moment(new Date()).format('YYYY-MM-DD')];
      let technician = { role: dictCode.SERVICE_CONSULTANT };
      const technicianRes = await searchRoleByCode(technician);
      // 服务顾问
      this.columns.find(x => x.dataIndex === 'SERVICE_ADVISOR').editItems = technicianRes.data.map(x => ({ value: x.userId + '', text: x.employeeName }));
      this.topFilterList.find(item => item.fieldName === 'serviceAdvisor').itemList = technicianRes.data.map(x => ({ text: x.employeeName, value: x.userId }));
    },
    closeHandler() {
      this.itemPart = false;
      this.completed = false;
    },
    closeDialog() {
      this.hour = false;
      this.labour = false;
    },
    // 获取品牌 ——下拉框
    async getBrand() {
      // 获取维修类型
      this.topFilterList.find(item => item.fieldName === 'repairtype').itemList = (await getfindAllRepairTypeList()).data.map(x => ({ text: x.REPAIR_TYPE_NAME, value: x.REPAIR_TYPE_CODE }));
      this.topFilterList.find(item => item.fieldName === 'brand').itemList = (await getPinpSelect()).data.map(x => ({ value: x.brandId + '', text: x.brandName }));
    },
    // 车系
    async getSeries(brandsid) {
      const res = await getChexSelect(brandsid);
      this.topFilterList.find(item => item.fieldName === 'series').itemList = res.data.map(x => ({ value: x.x.seriesId + '', text: x.seriesName }));
    },
    // 车型
    async getModel(brandsid, seriessid) {
      const res = await getCheModelSelect(brandsid, seriessid);
      this.topFilterList.find(item => item.fieldName === 'model').itemList = res.data.map(x => ({ value: x.x.modelId + '', text: x.modelName }));
    },

    // 品牌下拉框改变时
    changePinpSelect(id) {
      this.clearFormItem(this.topFilterList.find(item => item.fieldName === 'series'));
      this.clearFormItem(this.topFilterList.find(item => item.fieldName === 'model'));
      this.brandCode = id;
      if (!id) return;
      this.getSeries(id);
    },
    // 车系下拉框改变时
    changeChexSelect(id) {
      this.clearFormItem(this.topFilterList.find(item => item.fieldName === 'model'));
      if (!id) return;
      this.getModel(this.brandCode, id);
    },
    // 清空下拉框表当控件的值和下拉列表
    clearFormItem(item) {
      item.initialValue = undefined;
      item.itemList = [];
    }
  }
};
</script>

<style lang="less" scoped></style>
